import Common from '../../common/common.js';
import FriendListItemChecked from './FriendListItemChecked.js';
import ServiceNet from '../../servicenet.js';
import BaseList from '../base/BaseList.js';

const logger = Common.log("FriendListChecked");

function FriendListChecked () {
    BaseList.call(this);
    this.initData();
}
let prototype = Object.create(BaseList.prototype);
prototype.constructor = FriendListChecked;
FriendListChecked.prototype  = prototype;

//初始化好友数据
FriendListChecked.prototype.initData = function(){
    //获取用户列表，一次性拉取所有用户
    let userIDList = [];
    const netFriendList = (startIndex) => {
        logger.debug("拉取好友列表：startIndex", startIndex);
        ServiceNet.getFriendList(startIndex, (d) => {
            let nextStartIndex = d.nextStartIndex;
            //整理userID
            if (d.friendData.length > 0) {
                d.friendData.forEach((t, i) => {
                    userIDList.push(t.userId);
                })
                if(nextStartIndex != 0){
                    netFriendList(nextStartIndex);
                }else{
                    //全部获取完毕
                    this.setUserIDList(userIDList);
                }
            }
        });
    }
    netFriendList(0);
}

FriendListChecked.prototype.setUserIDList = function(userIDList){
    this.setItems( userIDList.map(t=>new FriendListItemChecked().updateDataByUserID(t)));
}

FriendListChecked.prototype.getCheckedUserID = function(){
    return this.items.filter(item=>item.checked).map(item=>new Object({userID:item.data.userID}));
}


export default FriendListChecked;
